<?php
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
require_once './Classes/PHPExcel.php';
$dbhost = '121.199.14.51';
$dbuser = 'root';
$dbpwd  ='sxy7788123456';
$dbName ='news';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
$query=$mysqli->query("SELECT * FROM plan_bak where status='on' order by sday  ");

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '姓名')
->setCellValue('B1', '任务')
->setCellValue('C1', '地点')
->setCellValue('D1', '开始时间')
->setCellValue('E1', '结束时间')
->setCellValue('F1', '状态')
;
$i=2;
while ($list =$query->fetch_array()){
    $tmpDay=explode('-',$list['endday']);
    $tmpTime=explode(':',$list['endtime']);
    $countEndtime=mktime($tmpTime[0],$tmpTime[1],0,$tmpDay[1],$tmpDay[2],$tmpDay[0]);
    $curTime=time();
    
    $tmpDay=$tmpTime=NULL;
    $tmpDay=explode('-',$list['startday']);
    $tmpTime=explode(':',$list['starttime']);
    $countStarttime=mktime($tmpTime[0],$tmpTime[1],0,$tmpDay[1],$tmpDay[2],$tmpDay[0]);
    
    $shen=0;
    $condition='距离';
    
    if($curTime < $countStarttime){  //距离多少天开始
        $condition='距离'; //距离
        $yong=0;   //用了0天
        $shen=floor(($countStarttime - $curTime) / (60*60*24));   //距离 多少天计算
        $yong=$shen;
    }else{
        if($curTime >= $countEndtime){
            $shen=floor(($curTime - $countEndtime) / (60*60*24));
            $condition='超出'; //超出
            $yong=$shen;
            //$yong=floor(($countEndtime - $countStarttime) / (60*60*24));  //用了多少天
        }else{
            $shen=floor(($countEndtime - $curTime) / (60*60*24));
            $condition='剩余'; //剩余
            $yong=floor(($curTime - $countStarttime) / (60*60*24));  //用了多少天
        }
    }
    
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$i, $list['name'])
    ->setCellValue('B'.$i, $list['task'])
    ->setCellValue('C'.$i, $list['place'])
    ->setCellValue('D'.$i, $list['startday'].$list['starttime'])
    ->setCellValue('E'.$i, $list['endday'].$list['endtime'])
    ->setCellValue('F'.$i, $condition.$yong."天")
    ;
    $i++;
}
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="进行中数据.xlsx"');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');